<link rel="import" href="../../node_modules/polymer-redux/dist/polymer-redux.html">
<script src="../../node_modules/redux/dist/redux.min.js"></script> 
<script>
    const initial = {
        machines: [],
        editMachines: []
    };

    const reducer = (state, action) => {
        if (!state) return initial;
        switch (action.type) {
            case 'ADD_MACHINE':
                // If state machine is undefined just initial it
                if (!state.machines) {
                    state.machines = initial.machines
                }
                console.log('addMachine', action.machines, state.machines)
                const {
                    $key,
                    ...newMachines
                } = action.machines
                const newerMachines = { ...newMachines,
                    mid: action.machines.$key
                };
                return {
                    ...state,
                    machines: [...state.machines, newerMachines]
                }
            case 'REMOVE_MACHINE':
                // console.log('removeMachine', action.index, state.machines)
                if (state.machines) {
                    return {
                        ...state,
                        machines: [
                            ...state.machines.slice(0, action.index),
                            ...state.machines.slice(action.index + 1)
                        ]
                    }
                }
                return state
            case 'EDIT_ADD_MACHINE':
                // If state machine is undefined just initial it
                if (!state.editMachines) {
                    state.editMachines = initial.editMachines
                } else if (state.editMachines) {
                    const {
                        $key,
                        ...newEditMachines
                    } = action.editMachines
                    const newerEditMachines = { ...newEditMachines,
                        mid: action.editMachines.$key
                    };
                    return {
                        ...state,
                        editMachines: [...state.editMachines, newerEditMachines]
                    }
                }
                return state
            case 'EDIT_REMOVE_MACHINE':
                if (state.editMachines) {
                    return {
                        ...state,
                        editMachines: [
                            ...state.editMachines.slice(0, action.index),
                            ...state.editMachines.slice(action.index + 1)
                        ]
                    }
                }
                return state
            case 'EDIT_SET_MACHINE':
                return {
                    ...state,
                    editMachines: action.editMachines
                }
            case 'CLEAR_MACHINE':
                return []
            default:
                return state
        }
    };
    const store = Redux.createStore(reducer, initial,
        Redux.compose(
            window.devToolsExtension ?
            window.devToolsExtension() :
            v => v)
    );
    const ReduxMixin = PolymerRedux(store);
</script> 